<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <!-- 准备好一个“容器” -->
  <div id="test"></div>
  <!-- 引入react核心库 -->
  <script type="text/javascript" src="../js/17.0.1/react.development.js"></script>
  <!-- 引入react-dom，用于支持react操作DOM -->
  <script type="text/javascript" src="../js/17.0.1/react-dom.development.js"></script>
  <!-- 引入babel，用于将jsx转为js -->
  <script type="text/javascript" src="../js/17.0.1/babel.min.js"></script>
  <script type="text/babel">
    class List extends React.Component {
      //初始化状态
      state = {
        newsArr: []
      }
      componentDidMount () {
        //模拟ajax请求
        setInterval(() => {
          const news = `新闻${this.state.newsArr.length + 1}`
          this.setState({
            newsArr: [news, ... this.state.newsArr]
          })
        }, 500)
      }

      getSnapshotBeforeUpdate () {
        return this.refs.listDom.scrollHeight
      }
      componentDidUpdate (preProps, preState, height) {
        this.refs.listDom.scrollTop += this.refs.listDom.scrollHeight - height
      }

      render () {
        return (
          <div className="list" ref='listDom'>
            {this.state.newsArr.map((item, i) => {
              return <div key={i}>{item}</div>
            })}
          </div>
        )
      }
    }
    ReactDOM.render(<List />, document.getElementById('test'))
  </script>
</body>
<style>
  .list {
    width: 100px;
    height: 100px;
    background-color: skyblue;
    overflow: auto;
  }
</style>

</html>